package com.blb.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;

import com.blb.entity.User;
import com.blb.entity.UserExample;
import com.blb.entity.UserExample.Criteria;
import com.blb.mapper.UserMapper;
import com.blb.utils.DateUtils;
import com.blb.utils.LayuiPage;
import com.blb.utils.MybatisUtils;
import com.mysql.jdbc.StringUtils;

public class UserService {

	private SqlSession session;
	
	/**
	 * 用户登录
	 * @param user
	 * @return
	 * @throws IOException
	 */
	public String login(User user) throws IOException{
		session = MybatisUtils.getSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andUsernameEqualTo(user.getUsername());
		
		List<User> list = userMapper.selectByExample(userExample);
		if(list.isEmpty()){
			return "用户名不存在";
		}
		
		if(!user.getPassword().equals(list.get(0).getPassword())){
			return "密码输入错误";
		}
		return "";
	}
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param username
	 * @return
	 * @throws IOException
	 */
	public LayuiPage page(int page,int limit,String username) throws IOException{
		
		session = MybatisUtils.getSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		
		//获取用户集合
		List<User> list = userMapper.page(page, limit, username);
		
		UserExample userExample = new UserExample();
		if(!StringUtils.isNullOrEmpty(username)){
			Criteria criteria = userExample.createCriteria();
			criteria.andUsernameLike("%"+ username + "%");
		}
		
		//获取总条数
		int count = (int) userMapper.countByExample(userExample);
		
		//生产json数据
		LayuiPage pagelist = new LayuiPage();
		pagelist.setData(list);
		pagelist.setCount(count);
		
		session.close();
		return pagelist;
	}
	
	/**
	 * 判断用户名是否存在
	 * @param username
	 * @throws IOException
	 */
	public List<User> isExist(String username) throws IOException{
		session = MybatisUtils.getSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<User> list = userMapper.selectByExample(userExample);
		
		session.close();
		
		return list;
	}
	
	/**
	 * 添加用户
	 * @param user
	 * @param httpsession
	 * @throws IOException
	 */
	public void add(User user) throws IOException{
		session = MybatisUtils.getSession();

		UserMapper userMapper = session.getMapper(UserMapper.class);
		userMapper.insertSelective(user);
		
		session.commit();
		session.close();
	}
	
	/**
	 * 修改
	 * @param user
	 * @throws IOException
	 */
	public void update(User user) throws IOException{
		session = MybatisUtils.getSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andIdEqualTo(user.getId());

		userMapper.updateByExampleSelective(user, userExample);
		
		session.commit();
		session.close();
	}
	
	/**
	 * 删除
	 * @param ids
	 * @throws IOException
	 */
	public void delete(String [] ids) throws IOException{
		session = MybatisUtils.getSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		for(String id : ids){
			userMapper.deleteByPrimaryKey(id);
		}
		
		session.commit();
		session.close();
	}
}
